﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrokerBase.Lib.Policy;

namespace BrokerBase.Lib.Product.AdjustRateConverter
{
    public abstract class AbstractAdjustRateConverter:IAdjustRateConverter
    {
        public virtual IAdjustRate Owner { get; set; }


        public virtual string Name { get; set; }


        public abstract double GetCoefficient(IPolicy policy, IAdjustRateItem selectedItem);


        public abstract IList<IAdjustRateItem> GetDefaultItems();


        public virtual IAdjustRateItem GetItemByPolicy(IPolicy policy)
        {
            KeyValuePair<string, string> k = policy.SelectedAdjRateItems.FirstOrDefault(p => p.Key == Owner.Name);

            string itemName=null;

            IAdjustRateItem item = null;

            if (!string.IsNullOrEmpty(k.Value))
            {
                itemName = k.Value;
                item = Owner.Items.First(p => p.Name == itemName);
            }

            if (Owner.Items.Count == 1)
            {
                item = Owner.Items[0];
                policy.SetSettingLock(Owner.Name, true);
            }

            return item;        
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;

            if (GetType() != obj.GetType())
                return false;

            IAdjustRateConverter converter = obj as IAdjustRateConverter;

            if (converter.Name == Name)
                return true;
            else
                return false;
        }

        public override int GetHashCode()
        {
            string str = GetType().Name + Name;

            return str.GetHashCode();
        }
    }
}
